import Vue from 'vue';
import axios from 'axios';
// import router from './router';

let count = 0;

axios.interceptors.request.use(config => {
  let url = config.url;
  /** 某些请求不用弹出加载动画
   * 例如：获取音乐mp3链接...等
   * */
  if(url.indexOf('/api/song/url?id=') != -1){
    return config;
  }
  if(url.indexOf('/api/lyric?id=') != -1){
    return config;
  }
  count++;
  Vue.$loading.open();
  return config;
}, error => {
  count = 0;
  Vue.$loading.close();
  alert("请求失败！");
  return Promise.reject(error);
});

axios.interceptors.response.use(response => {
  count--;
  if (count <= 0){
    count = 0;
    Vue.$loading.close();
  }
  // Vue.$loading.close();
  return response;
}, error => {
  count = 0;
  Vue.$loading.close();
  alert("请求失败！");
  // Vue.$loading.close();
  return Promise.reject(error);
});

export default axios;